/**
 * Mar 24, 2014 9:44:29 AM
 */
package elitech.vietnam.weatherfashion.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;

/**
 * @author Cong
 *
 */
public class NotifyButton extends LinearLayout implements View.OnClickListener {
	
	TextView mNumber;
	
	public NotifyButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public NotifyButton(Context context) {
		super(context);
	}
	
	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		setOnClickListener(this);
		mNumber = (TextView) findViewById(R.id.btnnoti_text);
		if (WFApplication.getInstance() != null)
			setNumber(WFApplication.getInstance().mBadge.mBadgeCount);
	}
	
	public void setNumber(int number) {
		if (mNumber == null)
			return;
		mNumber.setText(number + "");
		if (number <= 0 || WFApplication.getInstance().mHideLocalNoti)
			mNumber.setVisibility(View.INVISIBLE);
		else {
			mNumber.setVisibility(View.VISIBLE);
			mNumber.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.bounce_drop));
		}
	}

	@Override
	public void onClick(View v) {
		try {
			((MainActivity) getContext()).mNotiWindow.showAsDropDown(this);
			((MainActivity) getContext()).mNotiWindow.notifyDataChanged();
			setNumber(0);
			WFApplication.getInstance().mHideLocalNoti = true;
		} catch (ClassCastException e) {
			e.printStackTrace();
		}
	}
}
